﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Quartz;
using EmgrMvc.BLL;

namespace EmgrMvc.Models
{
    public class IRControlJob : IJob
    {
        public MyContext DbContext { get; set; }
        public List<Device> Devices { get; set; }
        public List<DeviceType> Types { get; set; }
        public string Action { get; set; } // ON/OFF
        MqttTool tool;

        public Task Execute(IJobExecutionContext context)
        {
            tool = MqttTool.getSingleton(); // new MqttTool();

            return Task.Factory.StartNew(() =>
            {
                Console.WriteLine($"Key：{context.JobDetail.Key}");
                foreach(Device dev in Devices)
                {
                    var type = Types.Single(t => t.Id == dev.Type);
                    string filename = (Action == "ON" ? type.DataFileOn : type.DataFileOff);
                    tool.ACControl(dev.ControllerNode + dev.Data, "Data/" + filename);
                }
            });

        }
    }
}
